package com.ky.manage.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.google.gson.Gson;
import com.ky.manage.BaseApplication;
import com.ky.manage.R;
import com.ky.manage.activity.GoodsDetailActivity;
import com.ky.manage.adapter.ProductsAdapter;
import com.ky.manage.base.BaseFragment;
import com.ky.manage.model.LoginUserInfo;
import com.ky.manage.model.response.ProductResp;
import com.ky.manage.utils.ZyUtils;
import com.ky.manage.utils.http.AsyncHttpUtils;
import com.loopj.android.http.TextHttpResponseHandler;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.ky.manage.utils.http.BusinessHttpReqUtils.SERVICE_DOMAIN;

public class ProductFragment extends BaseFragment {
    /**
     * value 0 is product(产品); value 1 is mountings(配件)
     */
    private int mType = 0;
    private GridView gridView;
    List<ProductResp.RowsDTO> products;
    ProductsAdapter adapter;

    public ProductFragment(){}

    public ProductFragment(int type) {
        mType = type;
    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_product;
    }

    @Override
    protected void initView(View rootView) {
        Log.d("lhm", "ProductFragment initView");
        gridView = findView(R.id.gv_product);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ProductResp.RowsDTO productResp = products.get(position);
                Intent intent = new Intent(getContext(), GoodsDetailActivity.class);
                intent.putExtra("id", productResp.getGoodsId());
                intent.putExtra("type", 1);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        adapter = new ProductsAdapter(mContext, new ArrayList<>());
        gridView.setAdapter(adapter);
        getProduct();
    }

    private void getProduct() {
        /*RequestParams httpParams = new RequestParams();
        httpParams.put("pageNum", mCurPageIndex);
        httpParams.put("pageSize", OverallData.REQ_PAGE_CNT);*/
        LoginUserInfo loginUserInfo = BaseApplication.getLoginUserInfo();
        if (null == loginUserInfo || !ZyUtils.getInstance().isNetConnected()) {
            return;
        }

        AsyncHttpUtils.getInstance().get(SERVICE_DOMAIN + "/appApi/getGoodsList?businessOutletsId=" + BaseApplication.getLoginUserInfo().businessOutletsId, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.d("lhm", "getProduct onFailure responseString = " + responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Log.d("lhm", "getProduct onSuccess responseString = " + responseString);

                ProductResp resp = new Gson().fromJson(responseString, ProductResp.class);
                if(resp.getCode() == 200) {
                    products = resp.getRows();
                    mHandler.sendEmptyMessage(1);

                }
            }
        });
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            adapter.setData(products);
        }
    };
}
